package com.wkg;

class count{
     volatile int count;
    public  void increase(){
        synchronized(this){
            count++;
        }


    }
}

public class test_101 {

    public static void main(String[] args) throws InterruptedException {
        count count = new count();
        Thread th1 = new Thread(() -> {
            for (int i = 0; i < 500000; i++) {
                count.increase();
            }
        });
        Thread th2 = new Thread(() -> {
            for (int i = 0; i < 500000; i++) {
                count.increase();
            }
        });
        th1.start();
        th2.start();
        th1.join();
        th2.join();
        System.out.println(count.count);

    }















    public static void main2(String[] args) throws InterruptedException {
        Thread thread = new Thread(() -> {
            for (int i = 0; i < 14; i++) {
                try {
                    System.out.println("hello thread...");
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }

            }
            System.out.println("线程退出");
        });

        System.out.println("启动线程之前是否状态" + thread.getState());
        thread.start();
        Thread.sleep(1000);
        System.out.println("启动线程中是否状态" + thread.getState());
        //thread.interrupt();
        Thread.sleep(100);
        System.out.println("启动线程中休眠时的状态" + thread.getState());

        thread.wait();
        thread.join();
        System.out.println("启动线程结束的状态" + thread.getState());
    }




    public static void main1(String[] args) throws InterruptedException {
        Thread thread = new Thread(() -> {
            for (int i = 0; i < 4; i++) {
                System.out.println("hello thread...");
                try {
                    Thread.sleep(1000);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            System.out.println("线程已完成");
        });

        System.out.println("启动线程之前线程是否存活" + thread.isAlive());
        thread.start();
        System.out.println("启动线程中线程是否存活" + thread.isAlive());
        thread.join();
        Thread.sleep(1000);
        System.out.println("启动线程之后线程是否存活" + thread.isAlive());

    }
}
